/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings;

import static com.google.android.tv.settings.TvSettings.System.SCREEN_DIM_TIMEOUT;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import static android.provider.Settings.System.SOUND_EFFECTS_ENABLED;
import com.android.settings.R;
import com.google.tv.preference.PreferenceActivity;
import com.hisense.settings.util.Wrapper;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.provider.Settings;
import android.preference.PreferenceScreen;
import android.preference.PreferenceManager; 
import android.content.SharedPreferences;

import java.util.Calendar;
import java.util.List;
import android.util.Log;

/**
 * Handles settings menu for image settings.  
 *
 * @author huangjiufa
 */
public class PowerOnRepeat extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener{
   
   private static final String TAG = "PowerOnRepeat";

    private static final String KEY_USE_ON_TIME = "use_on_time";
    private static final String KEY_ONCE = "once";
    private static final String KEY_WEEK_MONDAY = "week_monday";
    private static final String KEY_WEEK_TUESDAY = "week_tuesday";
    private static final String KEY_WEEK_WEDNESDAY = "week_wednesday";
    private static final String KEY_WEEK_THURSDAY = "week_thursday";
    private static final String KEY_WEEK_FRIDAY = "week_friday";
    private static final String KEY_WEEK_SATURDAY = "week_saturday";
    private static final String KEY_WEEK_SUNDAY = "week_sunday";
	
    private CheckBoxPreference mUseOnTimePreference;
	private CheckBoxPreference mOncePreference;
	private CheckBoxPreference mWeekMondayPreference;
	private CheckBoxPreference mWeekTuesdayPreference;
	private CheckBoxPreference mWeekWednesdayPreference;
	private CheckBoxPreference mWeekThursdayPreference;
	private CheckBoxPreference mWeekFridayPreference;
	private CheckBoxPreference mWeekSaturdayPreference;
	private CheckBoxPreference mWeekSundayPreference;
	private int timer;
	private int values;
	private Wrapper mwrapper=null;
	SharedPreferences.Editor editor;


/*
	private class IntPrefListener
		  implements Preference.OnPreferenceChangeListener {
	  private String mKey;

	  public IntPrefListener(String key) {
		  mKey = key;
	  }

	  public boolean onPreferenceChange(Preference preference,
			  Object newValue) {
		  String key = preference.getKey();
		  int value;
		  try {
			  value = Integer.parseInt((String) newValue);
		  } catch (NumberFormatException e) {
			  return false;
		  }
		  Settings.System.putInt(getContentResolver(), mKey,
				  value);
		  return true;
	  }
	}
*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.power_on_repeat);
        SharedPreferences mSetPowerOfftimer = getSharedPreferences("com.android.settings_preferences", Context.MODE_PRIVATE);
        
        Calendar c =Calendar.getInstance();
int mday=c.get(Calendar.DAY_OF_MONTH);
int mHour =c.get(Calendar.HOUR_OF_DAY);
int mMinute=c.get(Calendar.MINUTE);

        int poweron_day=mSetPowerOfftimer.getInt("poweron_day", 0);
        int poweron_hour=mSetPowerOfftimer.getInt("poweron_hour", 12);
        int poweron_minute=mSetPowerOfftimer.getInt("poweron_minute", 0);
        timer=(poweron_day-mday)*24*60+(poweron_hour-mHour)*60+poweron_minute-mMinute;
        values=(12-mHour)*60-mMinute;
        mwrapper = Wrapper.getWrapperInstance();
        Log.i("values", values+"@@@@@@@@@@@@@@@@@@@@@");
		Log.i("mHour", mHour+"&&&&&&&&&&&&&&&");
        
        mUseOnTimePreference = (CheckBoxPreference) findPreference(KEY_USE_ON_TIME);
        //final int currentThreeDMode = getThreeDMode(VALUE_DEFAULT_THREED_MODE);
        mUseOnTimePreference.setOnPreferenceChangeListener(this);
		
		mOncePreference = (CheckBoxPreference) findPreference(KEY_ONCE);
        //final int currentThreeLRSwitch = getThreeDLRSwitch(VALUE_DEFAULT_THREED_MODE);
        mOncePreference.setOnPreferenceChangeListener(this);

        mWeekMondayPreference = (CheckBoxPreference) findPreference(KEY_WEEK_MONDAY);
        mWeekMondayPreference.setOnPreferenceChangeListener(this);

		mWeekTuesdayPreference= (CheckBoxPreference) findPreference(KEY_WEEK_TUESDAY);
        mWeekTuesdayPreference.setOnPreferenceChangeListener(this);

		mWeekWednesdayPreference= (CheckBoxPreference) findPreference(KEY_WEEK_WEDNESDAY);
        mWeekWednesdayPreference.setOnPreferenceChangeListener(this);

        mWeekThursdayPreference= (CheckBoxPreference) findPreference(KEY_WEEK_THURSDAY);
        mWeekThursdayPreference.setOnPreferenceChangeListener(this);

        mWeekFridayPreference= (CheckBoxPreference) findPreference(KEY_WEEK_FRIDAY);
        mWeekFridayPreference.setOnPreferenceChangeListener(this);

		mWeekSaturdayPreference= (CheckBoxPreference) findPreference(KEY_WEEK_SATURDAY);
        mWeekSaturdayPreference.setOnPreferenceChangeListener(this);

        mWeekSundayPreference= (CheckBoxPreference) findPreference(KEY_WEEK_SUNDAY);
        mWeekSundayPreference.setOnPreferenceChangeListener(this);

    }
    
    @Override
    public void onResume() {
       super.onResume();
       SharedPreferences mSetPowerOntimer = PreferenceManager.getDefaultSharedPreferences(this);
       Boolean use_on_time=mSetPowerOntimer.getBoolean("use_on_time",false);
       Boolean once=mSetPowerOntimer.getBoolean(KEY_ONCE, false);
	   Boolean monday=mSetPowerOntimer.getBoolean(KEY_WEEK_MONDAY, false);
	   Boolean tuesday=mSetPowerOntimer.getBoolean(KEY_WEEK_TUESDAY, false);
	   Boolean wednesday=mSetPowerOntimer.getBoolean(KEY_WEEK_WEDNESDAY, false);
	   Boolean thursday=mSetPowerOntimer.getBoolean(KEY_WEEK_THURSDAY, false);
	   Boolean friday=mSetPowerOntimer.getBoolean(KEY_WEEK_FRIDAY, false);
	   Boolean saturday=mSetPowerOntimer.getBoolean(KEY_WEEK_SATURDAY, false);
	   Boolean sunday=mSetPowerOntimer.getBoolean(KEY_WEEK_SUNDAY, false);
       if(use_on_time==false){
    		
			if(mOncePreference.isChecked())
				mOncePreference.setChecked(false);
			mOncePreference.setEnabled(false);
			
			if(mWeekMondayPreference.isChecked())
				mWeekMondayPreference.setChecked(false);
			mWeekMondayPreference.setEnabled(false);

			if(mWeekTuesdayPreference.isChecked())
				mWeekTuesdayPreference.setChecked(false);
			mWeekTuesdayPreference.setEnabled(false);

			if(mWeekWednesdayPreference.isChecked())
				mWeekWednesdayPreference.setChecked(false);
			mWeekWednesdayPreference.setEnabled(false);

			if(mWeekThursdayPreference.isChecked())
				mWeekThursdayPreference.setChecked(false);
			mWeekThursdayPreference.setEnabled(false);

			if(mWeekFridayPreference.isChecked())
				mWeekFridayPreference.setChecked(false);
			mWeekFridayPreference.setEnabled(false);

			if(mWeekSaturdayPreference.isChecked())
				mWeekSaturdayPreference.setChecked(false);
			mWeekSaturdayPreference.setEnabled(false);

			if(mWeekSundayPreference.isChecked())
				mWeekSundayPreference.setChecked(false);
			mWeekSundayPreference.setEnabled(false);		
       }
       else
       {
			mOncePreference.setEnabled(true);
//			mOncePreference.setChecked(true);
            mWeekMondayPreference.setEnabled(true);
			mWeekTuesdayPreference.setEnabled(true);
			mWeekWednesdayPreference.setEnabled(true);
			mWeekThursdayPreference.setEnabled(true);
			mWeekFridayPreference.setEnabled(true);
			mWeekSaturdayPreference.setEnabled(true);
			mWeekSundayPreference.setEnabled(true);	

			if(monday==false&&tuesday==false&&wednesday==false&&thursday==false
				&&friday==false&&saturday==false&&sunday==false)
				{
				mOncePreference.setEnabled(true);
				mOncePreference.setChecked(true);
				once=true;
				Log.i("11111111",once+"");
				}
			if(once==true){
				if(timer<=0){
					mwrapper.setWakeupTimer(values);
				}
				if(mWeekMondayPreference.isChecked())
					mWeekMondayPreference.setChecked(false);
				mWeekMondayPreference.setEnabled(false);

				if(mWeekTuesdayPreference.isChecked())
					mWeekTuesdayPreference.setChecked(false);
				mWeekTuesdayPreference.setEnabled(false);

				if(mWeekWednesdayPreference.isChecked())
					mWeekWednesdayPreference.setChecked(false);
				mWeekWednesdayPreference.setEnabled(false);

				if(mWeekThursdayPreference.isChecked())
					mWeekThursdayPreference.setChecked(false);
				mWeekThursdayPreference.setEnabled(false);

				if(mWeekFridayPreference.isChecked())
					mWeekFridayPreference.setChecked(false);
				mWeekFridayPreference.setEnabled(false);

				if(mWeekSaturdayPreference.isChecked())
					mWeekSaturdayPreference.setChecked(false);
				mWeekSaturdayPreference.setEnabled(false);

				if(mWeekSundayPreference.isChecked())
					mWeekSundayPreference.setChecked(false);
				mWeekSundayPreference.setEnabled(false);
				
			}
       }
       
       }

	
    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
		Log.d(TAG,  "onPreferenceTreeClick key:");
		
		//power on tv
		mwrapper = Wrapper.getWrapperInstance();
	    SharedPreferences mSetPowerOntimer = getSharedPreferences("com.android.settings_preferences", Context.MODE_PRIVATE);		
		editor = mSetPowerOntimer.edit();		
		editor.putLong("current_time", System.currentTimeMillis());
		editor.commit();

		PowerOnTimer pr = new PowerOnTimer();
	    long ontime=pr.Powerontime(mSetPowerOntimer, pr.getPoweronTime());
	    int power_on=(int)ontime/60000;						
	    mwrapper.setWakeupTimer(power_on);
		return false;
    }

    public boolean onPreferenceChange(Preference preference, Object objValue) {
        final String key = preference.getKey();
		Log.d(TAG,  "onPreferenceChange key:"+key+ "objValue:"+objValue);
        if (KEY_USE_ON_TIME.equals(key)) {
			Log.d(TAG,  "KEY_USE_ON_TIME == key:");
            boolean value = (Boolean)objValue;
            try {
				if(value)
				{
					mOncePreference.setEnabled(true);
					mOncePreference.setChecked(true);
	                mWeekMondayPreference.setEnabled(false);
					mWeekTuesdayPreference.setEnabled(false);
					mWeekWednesdayPreference.setEnabled(false);
					mWeekThursdayPreference.setEnabled(false);
					mWeekFridayPreference.setEnabled(false);
					mWeekSaturdayPreference.setEnabled(false);
					mWeekSundayPreference.setEnabled(false);

				}
				else
				{	
					if(mOncePreference.isChecked())
						mOncePreference.setChecked(false);
					mOncePreference.setEnabled(false);
					
					if(mWeekMondayPreference.isChecked())
						mWeekMondayPreference.setChecked(false);
					mWeekMondayPreference.setEnabled(false);

					if(mWeekTuesdayPreference.isChecked())
						mWeekTuesdayPreference.setChecked(false);
					mWeekTuesdayPreference.setEnabled(false);

					if(mWeekWednesdayPreference.isChecked())
						mWeekWednesdayPreference.setChecked(false);
					mWeekWednesdayPreference.setEnabled(false);

					if(mWeekThursdayPreference.isChecked())
						mWeekThursdayPreference.setChecked(false);
					mWeekThursdayPreference.setEnabled(false);

					if(mWeekFridayPreference.isChecked())
						mWeekFridayPreference.setChecked(false);
					mWeekFridayPreference.setEnabled(false);

					if(mWeekSaturdayPreference.isChecked())
						mWeekSaturdayPreference.setChecked(false);
					mWeekSaturdayPreference.setEnabled(false);

					if(mWeekSundayPreference.isChecked())
						mWeekSundayPreference.setChecked(false);
					mWeekSundayPreference.setEnabled(false);
				}
				

            } catch (NumberFormatException e) {
                Log.e(TAG, "could not use on time setting", e);
            }
        }
		else if (KEY_ONCE.equals(key)) {
            boolean value =  (Boolean)objValue;
            try {
				if(value)
				{
					if(timer<=0){
						mwrapper.setWakeupTimer(values);
					}
					if(mWeekMondayPreference.isChecked())
						mWeekMondayPreference.setChecked(false);
					mWeekMondayPreference.setEnabled(false);

					if(mWeekTuesdayPreference.isChecked())
						mWeekTuesdayPreference.setChecked(false);
					mWeekTuesdayPreference.setEnabled(false);

					if(mWeekWednesdayPreference.isChecked())
						mWeekWednesdayPreference.setChecked(false);
					mWeekWednesdayPreference.setEnabled(false);

					if(mWeekThursdayPreference.isChecked())
						mWeekThursdayPreference.setChecked(false);
					mWeekThursdayPreference.setEnabled(false);

					if(mWeekFridayPreference.isChecked())
						mWeekFridayPreference.setChecked(false);
					mWeekFridayPreference.setEnabled(false);

					if(mWeekSaturdayPreference.isChecked())
						mWeekSaturdayPreference.setChecked(false);
					mWeekSaturdayPreference.setEnabled(false);

					if(mWeekSundayPreference.isChecked())
						mWeekSundayPreference.setChecked(false);
					mWeekSundayPreference.setEnabled(false);

				}
				else
				{
	                mWeekMondayPreference.setEnabled(true);
					mWeekTuesdayPreference.setEnabled(true);
					mWeekWednesdayPreference.setEnabled(true);
					mWeekThursdayPreference.setEnabled(true);
					mWeekFridayPreference.setEnabled(true);
					mWeekSaturdayPreference.setEnabled(true);
					mWeekSundayPreference.setEnabled(true);
				}
				

            } catch (NumberFormatException e) {
                Log.e(TAG, "could not once setting", e);
            }
        }

        return true;
    }

}
